﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Reports_ServiceInvoice_InvoiceViews_Bills : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    AccountSettings acntstng = new AccountSettings();
    DataTable dtMain = new DataTable();
    DataTable dtMain1 = new DataTable();
    DataTable dtMain2 = new DataTable();
    string finyear = "";
    Double totalTaxable = 0;
    Double totalNonTaxable = 0;
    Double totalTax = 0;
    Double totalPECess = 0;
    Double totalHECess = 0;
    Double totalServiceTax = 0;
    Double totalServCharge = 0;
    Double totalGross = 0;
    Double totalReiumbersment = 0;
    Double totalVAT = 0;
    Double totalGrossTaxable = 0;
    Double totalGrossNonTaxable = 0;
    Double totalTaxableDiscount = 0;
    Double totalNonTaxableDiscount = 0;
    Double totalAmountReceived = 0;
    Double totalGrossN = 0;
    Double totalServChargeN = 0;
    Double totalTaxN = 0;
    Double totalNetAmt = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            finyear = Session["finyear"].ToString();
            hidfinyear.Value = Session["finyear"].ToString();
            txtFromDate.Text = Convert.ToString("01/04/" + finyear);
            txtToDate.Text = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
            BindGrid(txtFromDate.Text, txtToDate.Text);
            lblCompName.Text = Session["compname"].ToString();
            lblCompName2.Text = Session["compname"].ToString();
        }
    }

    protected void BindGrid(string from,string to)
    {
        if (from != string.Empty && to != string.Empty)
        {
            SqlParameter[] sqlparam = {
                                      new SqlParameter("@Finyear",hidfinyear.Value.ToString()),
                                      new SqlParameter("@FROMDATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(from))),
                                      new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(to))),
                                  };
           
            dtMain1 = DU.getdatausingprocedure("USP_IVC_GetBillwiseReceiptsForBills", sqlparam);
            dtMain2 = DU.getdatausingprocedure("USP_IVC_GetBillwiseReceiptsForDN", sqlparam);
            dtMain1.Merge(dtMain2);
        }
        lblMsg.Text = "Invoice Register : From " +string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(from)) + " To " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(to)) + ".";
        lblMsg2.Text = "Invoice Register : From " + from.ToString() + " To " + to.ToString() + ".";
        grdBillView.DataSource = dtMain1;
        grdBillView.DataBind();
      // GridView d = new GridView();
        grdPrint.DataSource = grdBillView.DataSource; //dtMain1;
        grdPrint.DataBind();
    }

    protected void grdBillView_RowDataBound(object sender, GridViewRowEventArgs  e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            if (chkbtnlstReceipt.Checked == true)
            {
                e.Row.Cells[18].Visible = true;
                e.Row.Cells[19].Visible = true;
                e.Row.Cells[20].Visible = true;
            }
            else
            {
                e.Row.Cells[18].Visible = false;
                e.Row.Cells[19].Visible = false;
                e.Row.Cells[20].Visible = false;
            }
            if (chkbtnlstDiscount.Checked == true)
            {
                e.Row.Cells[4].Visible = true;
                e.Row.Cells[5].Visible = true;
                e.Row.Cells[7].Visible = true;
                e.Row.Cells[8].Visible = true;
            }
            else
            {
                e.Row.Cells[4].Visible = false;
                e.Row.Cells[5].Visible = false;
                e.Row.Cells[7].Visible = false;
                e.Row.Cells[8].Visible = false;
            }
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (chkbtnlstReceipt.Checked == true)
            {
                e.Row.Cells[18].Visible = true;
                e.Row.Cells[19].Visible = true;
                e.Row.Cells[20].Visible = true;
            }
            else
            {
                e.Row.Cells[18].Visible = false;
                e.Row.Cells[19].Visible = false;
                e.Row.Cells[20].Visible = false;
            }
            if (chkbtnlstDiscount.Checked == true)
            {
                e.Row.Cells[4].Visible = true;
                e.Row.Cells[5].Visible = true;
                e.Row.Cells[7].Visible = true;
                e.Row.Cells[8].Visible = true;
            }
            else
            {
                e.Row.Cells[4].Visible = false;
                e.Row.Cells[5].Visible = false;
                e.Row.Cells[7].Visible = false;
                e.Row.Cells[8].Visible = false;
            }

            Double TaxableAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TaxableAmount"));
            Double NonTaxableAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "NonTaxableAmount"));
            Double ReimbursementAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReimbursementAmount"));
            Double ServiceTax1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ServiceTax1"));
            Double PECess1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "PECess1"));
            Double HECess1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "HECess1"));
            Double VATAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "VATAmount"));
            Double SurCharge = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "SurCharge"));
            Double TotalTax = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TotalTax"));
            Double Gross = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Gross"));
            Double GrossTaxable = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "GrossTaxable"));
            Double GrossNonTaxable = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "GrossNonTaxable"));
            Double DiscountTaxable = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TaxableDiscount"));
            Double DiscountNonTaxable = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "NonTaxableDiscount"));
            Double AmountReceived = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "AmountReceived"));

            totalGross = totalGross + Math.Abs(Gross);
            totalHECess = totalHECess + Math.Abs(HECess1);
            totalPECess = totalPECess + Math.Abs(PECess1);
            totalReiumbersment = totalReiumbersment + Math.Abs(ReimbursementAmount);
            totalServCharge = totalServCharge + Math.Abs(SurCharge);
            totalServiceTax = totalServiceTax + Math.Abs(ServiceTax1);
            totalTax = totalTax + Math.Abs(TotalTax);
            totalTaxable = totalTaxable + Math.Abs(TaxableAmount);
            totalNonTaxable = totalNonTaxable + Math.Abs(NonTaxableAmount);
            totalVAT = totalVAT + Math.Abs(VATAmount);
            totalAmountReceived = totalAmountReceived + Math.Abs(AmountReceived);
            totalGrossNonTaxable = totalGrossNonTaxable + Math.Abs(GrossNonTaxable);
            totalGrossTaxable = totalGrossTaxable + Math.Abs(GrossTaxable);
            totalTaxableDiscount = totalTaxableDiscount + Math.Abs(DiscountTaxable);
            totalNonTaxableDiscount = totalNonTaxableDiscount + Math.Abs(DiscountNonTaxable);

            if (Math.Abs(GrossTaxable) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(GrossTaxable));
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }
            if (Math.Abs(DiscountTaxable) > 0)
            {
                e.Row.Cells[5].Text = acntstng.GetCurrency(Math.Abs(DiscountTaxable));
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;
            }
            if (Math.Abs(TaxableAmount) > 0)
            {
                e.Row.Cells[6].Text = acntstng.GetCurrency(Math.Abs(TaxableAmount));
            }
            else
            {
                e.Row.Cells[6].Text = string.Empty;
            }
            if (Math.Abs(GrossNonTaxable) > 0)
            {
                e.Row.Cells[7].Text = acntstng.GetCurrency(Math.Abs(GrossNonTaxable));
            }
            else
            {
                e.Row.Cells[7].Text = string.Empty;
            }
            if (Math.Abs(DiscountNonTaxable) > 0)
            {
                e.Row.Cells[8].Text = acntstng.GetCurrency(Math.Abs(DiscountNonTaxable));
            }
            else
            {
                e.Row.Cells[8].Text = string.Empty;
            }
            if (Math.Abs(NonTaxableAmount) > 0)
            {
                e.Row.Cells[9].Text = acntstng.GetCurrency(Math.Abs(NonTaxableAmount));
            }
            else
            {
                e.Row.Cells[9].Text = string.Empty;
            }
            if (Math.Abs(ReimbursementAmount) > 0)
            {
                e.Row.Cells[10].Text = acntstng.GetCurrency(Math.Abs(ReimbursementAmount));
            }
            else
            {
                e.Row.Cells[10].Text = string.Empty;
            }
            if (Math.Abs(ServiceTax1) > 0)
            {
                e.Row.Cells[11].Text = acntstng.GetCurrency(Math.Abs(ServiceTax1));
            }
            else
            {
                e.Row.Cells[11].Text = string.Empty;
            }
            if (Math.Abs(PECess1) > 0)
            {
                e.Row.Cells[12].Text = acntstng.GetCurrency(Math.Abs(PECess1));
            }
            else
            {
                e.Row.Cells[12].Text = string.Empty;
            }
            if (Math.Abs(HECess1) > 0)
            {
               e.Row.Cells[13].Text = acntstng.GetCurrency(Math.Abs(HECess1));
            }
            else
            {
                e.Row.Cells[13].Text = string.Empty;
            }
            if (Math.Abs(VATAmount) > 0)
            {
                e.Row.Cells[14].Text = acntstng.GetCurrency(Math.Abs(VATAmount));
            }
            else
            {
                e.Row.Cells[14].Text = string.Empty;
            }
            if (Math.Abs(SurCharge) > 0)
            {
                e.Row.Cells[15].Text = acntstng.GetCurrency(Math.Abs(SurCharge));
            }
            else
            {
                e.Row.Cells[15].Text = string.Empty;
            }
            if (Math.Abs(TotalTax) > 0)
            {
                e.Row.Cells[16].Text = acntstng.GetCurrency(Math.Abs(TotalTax));
            }
            else
            {
                e.Row.Cells[16].Text = string.Empty;
            }
            if (Math.Abs(Gross) > 0)
            {
                e.Row.Cells[17].Text = acntstng.GetCurrency(Math.Abs(Gross));
            }
            else
            {
                e.Row.Cells[17].Text = string.Empty;
            }
            if (Math.Abs(AmountReceived) > 0)
            {
                e.Row.Cells[20].Text = acntstng.GetCurrency(Math.Abs(AmountReceived));
            }
            else
            {
                e.Row.Cells[20].Text = string.Empty;
            }
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[12].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[13].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[14].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[15].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[16].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[17].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[20].HorizontalAlign = HorizontalAlign.Right;
      
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            if (chkbtnlstReceipt.Checked == true)
            {
                e.Row.Cells[18].Visible = true;
                e.Row.Cells[19].Visible = true;
                e.Row.Cells[20].Visible = true;
            }
            else
            {
                e.Row.Cells[18].Visible = false;
                e.Row.Cells[19].Visible = false;
                e.Row.Cells[20].Visible = false;
            }
            if (chkbtnlstDiscount.Checked == true)
            {
                e.Row.Cells[4].Visible = true;
                e.Row.Cells[5].Visible = true;
                e.Row.Cells[7].Visible = true;
                e.Row.Cells[8].Visible = true;
            }
            else
            {
                e.Row.Cells[4].Visible = false;
                e.Row.Cells[5].Visible = false;
                e.Row.Cells[7].Visible = false;
                e.Row.Cells[8].Visible = false;
            }

            e.Row.Cells[3].Text = "Total";
            e.Row.Cells[4].Text = acntstng.GetCurrency(totalGrossTaxable).ToString();
            e.Row.Cells[5].Text = acntstng.GetCurrency(totalTaxableDiscount).ToString();
            e.Row.Cells[6].Text = acntstng.GetCurrency(totalTaxable).ToString();
            e.Row.Cells[7].Text = acntstng.GetCurrency(totalGrossNonTaxable).ToString();
            e.Row.Cells[8].Text = acntstng.GetCurrency(totalNonTaxableDiscount).ToString();
            e.Row.Cells[9].Text = acntstng.GetCurrency(totalNonTaxable).ToString();
            e.Row.Cells[10].Text = acntstng.GetCurrency(totalReiumbersment).ToString();
            e.Row.Cells[11].Text = acntstng.GetCurrency(totalServiceTax).ToString();
            e.Row.Cells[12].Text = acntstng.GetCurrency(totalPECess).ToString();
            e.Row.Cells[13].Text = acntstng.GetCurrency(totalHECess).ToString();
            e.Row.Cells[14].Text = acntstng.GetCurrency(totalVAT).ToString();
            e.Row.Cells[15].Text = acntstng.GetCurrency(totalServCharge).ToString();
            e.Row.Cells[16].Text = acntstng.GetCurrency(totalTax).ToString();
            e.Row.Cells[17].Text = acntstng.GetCurrency(totalGross).ToString();
            e.Row.Cells[20].Text = acntstng.GetCurrency(totalAmountReceived).ToString();
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
        }
        
    }

    protected void btnCangeDate_Click(object sender, EventArgs e)
    {
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }
    protected void grdBillView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdBillView.PageIndex =e.NewPageIndex;
        grdPrint.PageIndex = e.NewPageIndex;
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }

    protected void chkbtnlstReceipt_CheckedChanged(object sender, EventArgs e)
    {
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }
    protected void chkbtnlstDiscount_CheckedChanged(object sender, EventArgs e)
    {
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }

    protected void grdPrint_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double SurChargeN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "SurCharge"));
            Double TotalTaxN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TotalTax"));
            Double GrossN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Gross"));
            Double NetAmt = SurChargeN + TotalTaxN + GrossN;

            totalServChargeN = totalServCharge + Math.Abs(SurChargeN);
            totalGrossN = totalGross + Math.Abs(GrossN);
            totalTaxN = totalTax + Math.Abs(TotalTaxN);
            totalNetAmt = totalServChargeN + totalGrossN + totalTaxN;
            
            if (Math.Abs(SurChargeN) > 0)
            {
                e.Row.Cells[3].Text = acntstng.GetCurrency(Math.Abs(SurChargeN));
            }
            else
            {
                e.Row.Cells[3].Text = string.Empty;
            }
            if (Math.Abs(TotalTaxN) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(TotalTaxN));
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }
            if (Math.Abs(GrossN) > 0)
            {
                e.Row.Cells[5].Text = acntstng.GetCurrency(Math.Abs(GrossN));
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;
            }
            if (Math.Abs(NetAmt) > 0)
            {
                e.Row.Cells[6].Text = acntstng.GetCurrency(Math.Abs(NetAmt));
            }
            else
            {
                e.Row.Cells[6].Text = string.Empty;
            }
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[2].Text = "Total";
            e.Row.Cells[3].Text = acntstng.GetCurrency(totalServChargeN).ToString();
            e.Row.Cells[4].Text = acntstng.GetCurrency(totalTaxN).ToString();
            e.Row.Cells[5].Text = acntstng.GetCurrency(totalGrossN).ToString();
            e.Row.Cells[6].Text = acntstng.GetCurrency(totalNetAmt).ToString();
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
        }
    }
}
